package org.ztr.yanai.blog.constant;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/11/22 14:30
 * @Description: Redis键常量类，统一管理Redis键的命名规则
 * @Version: 1.0
 */
public class RedisKeyConstants {

    private RedisKeyConstants() {
        throw new IllegalStateException("Constant class");
    }

    /**
     * 用户相关键
     */
    public static class User {
        public static final String TOKEN_PREFIX = "user:token:";
        public static final String INFO_PREFIX = "user:info:";
        public static final String LOGIN_ATTEMPT = "user:login:attempt:";
        public static final String ONLINE_USERS = "user:online";

        public static String tokenKey(Long userId) {
            return TOKEN_PREFIX + userId;
        }

        public static String infoKey(Long userId) {
            return INFO_PREFIX + userId;
        }

        public static String loginAttemptKey(String username) {
            return LOGIN_ATTEMPT + username;
        }
    }

    /**
     * 文章相关键
     */
    public static class Post {
        public static final String VIEW_PREFIX = "post:view:";
        public static final String LIKE_PREFIX = "post:like:";
        public static final String COMMENT_PREFIX = "post:comment:";
        public static final String HOT_POSTS = "post:hot";
        public static final String RECENT_POSTS = "post:recent";

        public static String viewKey(Long postId) {
            return VIEW_PREFIX + postId;
        }

        public static String likeKey(Long postId) {
            return LIKE_PREFIX + postId;
        }

        public static String commentKey(Long postId) {
            return COMMENT_PREFIX + postId;
        }
    }

    /**
     * 验证码相关键
     */
    public static class Captcha {
        public static final String EMAIL_CODE_PREFIX = "captcha:email:";
        public static final String SMS_CODE_PREFIX = "captcha:sms:";
        public static final String IMAGE_CODE_PREFIX = "captcha:image:";

        public static String emailCodeKey(String email) {
            return EMAIL_CODE_PREFIX + email;
        }

        public static String smsCodeKey(String phone) {
            return SMS_CODE_PREFIX + phone;
        }

        public static String imageCodeKey(String sessionId) {
            return IMAGE_CODE_PREFIX + sessionId;
        }
    }

    /**
     * 限流相关键
     */
    public static class RateLimit {
        public static final String API_PREFIX = "ratelimit:api:";
        public static final String IP_PREFIX = "ratelimit:ip:";

        public static String apiKey(String apiPath, String userId) {
            return API_PREFIX + apiPath + ":" + userId;
        }

        public static String ipKey(String ip) {
            return IP_PREFIX + ip;
        }
    }

    /**
     * 缓存相关键
     */
    public static class Cache {
        public static final String CATEGORY_LIST = "cache:category:list";
        public static final String TAG_LIST = "cache:tag:list";
        public static final String STATISTICS = "cache:statistics";
    }
}
